<template>
  <!-- 页面整体容器 -->
  <div class="home-container">
    <!-- 头部区域 -->
    <header class="header">
      <!-- 左上角平台名称和logo -->
      <div class="header-logo">
        <img src="../../assets/photoes/logo.jpg" alt="logo" />
        <span class="system-name">铁岭电网无人机管理平台</span>
      </div>

      <!-- 右上角用户信息和退出功能 -->
      <div class="user-info">
        <el-dropdown @command="handleUserCommand">
          <span class="user-name">欢迎，{{ username }}</span>
          <template #dropdown>
            <el-dropdown-menu>
              <!-- 用户管理：仅管理员和超级管理员可见 -->
              <el-dropdown-item v-if="isAdmin" command="userManage">
                <el-icon>
                  <UserFilled />
                </el-icon>
                用户管理
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <el-icon>
                  <ArrowLeftBold />
                </el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>

      <!-- 头部主要信息区域 -->
      <div class="header-info">
        <p class="main-title">智驭无人机</p>
        <p class="sub-title">科技赋能无人机，作业智绘新蓝图</p>
        <!-- <el-button type="primary">了解详情</el-button> -->
      </div>
    </header>

    <!-- 功能列表区域 -->
    <section class="function-section">
      <h2 class="section-title">六大功能</h2>
      <div class="function-list">
        <div class="function-item" v-for="(item, index) in functionList" :key="index"
          @click="navigateToFunction(index)">
          <img :src="item.img" alt="img" class="function-img" />
          <p class="function-text">{{ item.name }}</p>
        </div>
      </div>
    </section>

    <!-- 底部区域 -->
    <!-- <footer class="footer">
      <div class="footer-top">
        <div class="footer-logo">
          <img src="../../assets/photoes/logo.jpg" alt="logo" />
          <span class="system-name">铁岭电网无人机管理平台</span>
        </div>
        <div class="contact-info">
          <div class="contact-item" v-for="(item, index) in contactInfo" :key="index">
            <img :src="item.img" alt="img" class="size-img" />
            <p>{{ item.message }}</p>
          </div>
        </div>
        <div class="qrcode">
          <img src="../../assets/photoes/home/qrcode.png" alt="qrcode" />
          <p>扫一扫关注我们</p>
        </div>
      </div>
      <div class="footer-bottom">
        <p>
          京公网安备 11010502031377 Copyright © 2005 ALL RIGHTS RESERVED
          XXXXX有限公司版权所有
        </p>
        <div class="bottom-right">
          <div class="link-list" v-for="(item, index) in linkList" :key="index">
            <a :href="item.link" target="_blank">{{ item.text }}</a>
          </div>
        </div>
      </div>
    </footer> -->
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, nextTick } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
const username = ref("");
const isAdmin = ref(false); // 控制用户管理是否显示

// 组件挂载时获取用户信息和角色
onMounted(() => {
  const userInfoStr = localStorage.getItem("userInfo");
  if (userInfoStr) {
    const userInfo = JSON.parse(userInfoStr);
    username.value = userInfo.name;

    // 判断用户角色是否为管理员或超级管理员
    isAdmin.value =
      userInfo.role === "超级管理员" || userInfo.role === "管理员";
  }

  // 恢复滚动位置
  nextTick(() => {
    const savedScrollTop = sessionStorage.getItem("homeScrollTop");
    if (savedScrollTop) {
      window.scrollTo(0, parseInt(savedScrollTop));
    }
  });

  // 添加滚动事件监听
  window.addEventListener("scroll", handleScroll);
});

// 页面卸载时移除事件监听
onUnmounted(() => {
  window.removeEventListener("scroll", handleScroll);
});

// 滚动事件处理函数（带防抖）
let scrollTimeout;
const handleScroll = () => {
  clearTimeout(scrollTimeout);
  scrollTimeout = setTimeout(() => {
    sessionStorage.setItem("homeScrollTop", window.scrollY.toString());
  }, 100);
};

const handleUserCommand = (command) => {
  if (command === "logout") {
    localStorage.removeItem("userInfo");
    localStorage.removeItem("token");
    ElMessage.success("退出登录成功");
    router.push("/");
  } else if (command === "userManage") {
    // 跳转到用户管理页面
    router.push({ name: "UserManage" });
  }
};

const functionList = ref([
  {
    img: require("@/assets/photoes/home/work.png"),
    name: "无人机作业工单管理平台",
    route: { name: "WorkOrder" },
  },
  {
    img: require("@/assets/photoes/home/car.png"),
    name: "无人机出入库管理系统",
    route: { name: "DroneWarehouseHome" },
  },
  {
    img: require("@/assets/photoes/home/picture.png"),
    name: "无人机回传照片整理程序",
    route: { name: "PictureManage" },
  },
  {
    img: require("@/assets/photoes/home/more.png"),
    name: "网络监控平台整合项目",
    route: { name: "VideoMonitor" }, // 新增路由配置
  },
  {
    img: require("@/assets/photoes/home/communication.png"),
    name: "输电专业知识学习平台",
    route: {
      name: "CourseTypes",
      query: { category: "输电专业" },
    },
  },
  {
    img: require("@/assets/photoes/home/cloud.png"),
    name: "输电线路无人机 5G 网联技术(图传管理)",
    route: { name: "CloudNetwork5G" },
  },
]);

// const contactInfo = ref([
//   {
//     img: require("@/assets/photoes/home/phone.png"),
//     message: "010 - 62209211",
//   },
//   {
//     img: require("@/assets/photoes/home/message.png"),
//     message: "ZCGS@BNU.EDU.CN",
//   },
//   {
//     img: require("@/assets/photoes/home/print.png"),
//     message: "010 - 62209211",
//   },
// ]);

// const linkList = ref([
//   { link: "https://www.baidu.com/", text: "隐私条款" },
//   { link: "https://www.baidu.com/", text: "联系我们" },
//   { link: "https://www.baidu.com/", text: "加入我们" },
// ]);

// 导航到功能页面
const navigateToFunction = (index) => {
  const functionItem = functionList.value[index];
  if (functionItem.route) {
    // 保存当前滚动位置
    sessionStorage.setItem("homeScrollTop", window.scrollY.toString());
    router.push(functionItem.route);
  }
};
</script>

<style scoped>
.home-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f7fa;
  overflow-x: hidden;
}

/* 头部区域样式优化 */
.header {
  width: 100%;
  height: 600px;
  background: url("../../assets/photoes/home/header-bg.jpg") center/cover no-repeat;
  position: relative;
  /* 增加半透明遮罩提升文字对比度 */
  box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.3);
}

/* 左上角平台名称 - 加深颜色并增加阴影 */
.header-logo {
  display: flex;
  align-items: center;
  position: absolute;
  top: 50px;
  left: 150px;
  z-index: 10;
}

.header-logo img {
  width: auto;
  height: 36px;
  /* 适当放大logo */
  margin-right: 12px;
}

.system-name {
  font-size: 22px;
  margin-left: 10px;
  color: #ffffff;
  /* 纯白色更醒目 */
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  /* 增加文字阴影提升可读性 */
  font-weight: 600;
}

/* 右上角用户信息 - 增强对比度 */
.user-info {
  position: absolute;
  top: 50px;
  right: 150px;
  z-index: 10;
}

.user-name {
  cursor: pointer;
  font-size: 16px;
  padding: 6px 12px;
  border-radius: 4px;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  background-color: rgba(0, 0, 0, 0.2);
  /* 增加背景透明度提升文字对比 */
  transition: all 0.3s;
}

.user-name:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

/* 头部主标题区域 - 加深颜色增强层次感 */
.header-info {
  position: absolute;
  top: 200px;
  left: 150px;
  width: 600px;
  /* 限制宽度避免文字过长 */
}

.main-title {
  font-size: 42px;
  margin-bottom: 15px;
  color: #ffffff;
  text-shadow: 0 3px 6px rgba(0, 0, 0, 0.5);
  font-weight: 700;
}

.sub-title {
  font-size: 20px;
  margin-bottom: 40px;
  color: #f0f0f0;
  /* 浅灰白色比纯白稍柔和但仍清晰 */
  line-height: 1.6;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

/* 功能列表区域 - 深色文字提升可读性 */
.function-section {
  padding: 60px 0;
}

.section-title {
  font-size: 30px;
  font-weight: 700;
  text-align: center;
  margin: 0 0 50px 0;
  color: #2c3e50;
  /* 深灰色更专业 */
  position: relative;
}

/* 给标题增加下划线装饰 */
.section-title::after {
  content: "";
  display: block;
  width: 80px;
  height: 4px;
  background-color: #199893;
  margin: 15px auto 0;
  border-radius: 2px;
}

.function-list {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  width: 85%;
  margin: 0 auto;
}

.function-item {
  width: 30%;
  text-align: center;
  margin-bottom: 35px;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 25px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  /* 增加轻微阴影 */
}

.function-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.function-img {
  width: 60px;
  /* 放大图标 */
  height: 60px;
  margin-bottom: 20px;
}

.function-text {
  color: #1a56db;
  /* 深蓝色更醒目 */
  font-weight: 600;
  font-size: 19px;
  line-height: 1.5;
}

/* 底部区域 - 加深文字颜色提升对比度 */
.footer {
  background: #2c3e50;
  /* 更深的背景色 */
  color: #e0e0e0;
  /* 浅灰色文字更清晰 */
  padding: 40px 0 20px;
  margin-top: 100px;
}

.footer-top {
  display: flex;
  justify-content: space-around;
  align-items: flex-start;
  /* 顶部对齐 */
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #3d5a7c;
}

.footer-logo {
  display: flex;
  align-items: center;
}

.footer-logo img {
  width: auto;
  height: 50px;
  margin-right: 12px;
}

.footer .system-name {
  color: #ffffff;
  font-size: 20px;
  font-weight: 600;
}

.contact-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.contact-item {
  display: flex;
  align-items: center;
}

.contact-item p {
  color: #e0e0e0;
  font-size: 15px;
  margin: 0;
}

.size-img {
  margin-right: 12px;
  width: 22px;
  height: 22px;
}

.qrcode {
  text-align: center;
}

.qrcode img {
  width: 120px;
  height: 120px;
  margin-bottom: 12px;
  border: 4px solid #ffffff;
  border-radius: 8px;
}

.qrcode p {
  color: #e0e0e0;
  font-size: 15px;
  margin: 0;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #3d5a7c;
}

.footer-bottom p {
  color: #b0b0b0;
  font-size: 14px;
  line-height: 1.6;
  margin: 0 0 15px;
}

.bottom-right {
  display: flex;
  justify-content: center;
  gap: 25px;
  margin-top: 15px;
}

.link-list a {
  color: #b0b0b0;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.link-list a:hover {
  color: #ffffff;
}
</style>
